package com.feeyin.radar.crm.controller;

import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.service.CustomerService;
import com.feeyin.radar.crm.service.ExceptionCustomerService;
import com.feeyin.radar.crm.service.MyRecycleBinService;
import com.feeyin.radar.crm.service.TeamRecycleBinService;
import com.feeyin.radar.sys.service.AssessmentTypeService;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

public class TeamRecycleBinController extends MultiActionController
{
  private TeamRecycleBinService teamRecycleBinService;
  private CustomerService customerService;
  private ExceptionCustomerService exceptionCustomerService;
  private MyRecycleBinService myRecycleBinService;
  private AssessmentTypeService assessmentTypeService;
  private String discardCustomer;

  public TeamRecycleBinService getTeamRecycleBinService()
  {
    return this.teamRecycleBinService;
  }

  public void setTeamRecycleBinService(TeamRecycleBinService teamRecycleBinService) {
    this.teamRecycleBinService = teamRecycleBinService;
  }

  public CustomerService getCustomerService() {
    return this.customerService;
  }

  public void setCustomerService(CustomerService customerService) {
    this.customerService = customerService;
  }

  public ExceptionCustomerService getExceptionCustomerService() {
    return this.exceptionCustomerService;
  }

  public void setExceptionCustomerService(ExceptionCustomerService exceptionCustomerService)
  {
    this.exceptionCustomerService = exceptionCustomerService;
  }

  public MyRecycleBinService getMyRecycleBinService() {
    return this.myRecycleBinService;
  }

  public void setMyRecycleBinService(MyRecycleBinService myRecycleBinService) {
    this.myRecycleBinService = myRecycleBinService;
  }

  public AssessmentTypeService getAssessmentTypeService() {
    return this.assessmentTypeService;
  }

  public void setAssessmentTypeService(AssessmentTypeService assessmentTypeService) {
    this.assessmentTypeService = assessmentTypeService;
  }

  public String getDiscardCustomer() {
    return this.discardCustomer;
  }

  public void setDiscardCustomer(String discardCustomer) {
    this.discardCustomer = discardCustomer;
  }

  public ModelAndView display(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    map.put("result", request.getParameter("result"));

    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String sFlag = request.getParameter("sFlag");
    String customerName = "";
    if ((!StringUtils.isEmpty(sFlag)) && ("1".equals(sFlag))) {
      customerName = request.getParameter("customerName");
    }
    map.put("sFlag", sFlag);
    map.put("customerName", customerName);

    Page page = new Page();
    page.getPageList(this.teamRecycleBinService.getPageDiscardCustomersCount(customerName, userId).intValue(), null, request);
    map.put("customers", this.teamRecycleBinService.getPageDiscardCustomers(customerName, userId, page));

    if (this.assessmentTypeService.isExpUser(userId)) {
      map.put("isExpUser", "1");
    }
    map.put("customerLimitForm", this.customerService.getCustomerLimit(userId));

    return new ModelAndView(getDiscardCustomer(), map);
  }

  public ModelAndView salePersonToCustomer(HttpServletRequest request, HttpServletResponse response)
  {
    String salePersonId = request.getParameter("salePersonId");
    boolean result = false;
    int success = 0;

    String[] ids = request.getParameterValues("id");
    if (!StringUtils.isEmpty(salePersonId)) {
      result = this.exceptionCustomerService.salePersonToCustomer(ids, Integer.valueOf(Integer.parseInt(salePersonId)));
      if (result) success = 1;
    }

    return new ModelAndView(new RedirectView("./teamRecycleBin.do?action=display&result=" + success));
  }

  public ModelAndView moveCustomerToOpenCustomer(HttpServletRequest request, HttpServletResponse response)
  {
    boolean result = false;
    int success = 0;

    String[] ids = request.getParameterValues("id");
    result = this.myRecycleBinService.turnToOpenCustomer(ids);
    if (result) success = 2;

    return new ModelAndView(new RedirectView("./teamRecycleBin.do?action=display&result=" + success));
  }

  public ModelAndView deleteCustomer(HttpServletRequest request, HttpServletResponse response)
  {
    boolean result = false;
    int success = 0;
    Integer userId = (Integer)request.getSession().getAttribute("userId");

    String[] ids = request.getParameterValues("id");
    result = this.teamRecycleBinService.deleteCustomer(ids, userId);
    if (result) success = 3;

    return new ModelAndView(new RedirectView("./teamRecycleBin.do?action=display&result=" + success));
  }
}